/**
 * Copyright (C) 2009, 2010 SC 4ViewSoft SRL
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package webview.test.chart;

import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import org.achartengine.tools.ZoomEvent;
import org.achartengine.tools.ZoomListener;

import webview.test.R;
import webview.test.common.ChartUtils;
import webview.test.common.Utils;
import webview.test.model.DailyStep;
import webview.test.model.LineChartInfo;
import webview.test.network.DigitalHealthClient;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.os.Bundle;

import android.view.WindowManager.LayoutParams;
import android.webkit.WebView;
import android.widget.LinearLayout;

public class ChartStepEasy2 extends Activity {

	// view
//	private GraphicalView chart;

	// 渲染器
//	private XYMultipleSeriesRenderer renderer;

	// 格式
	private final static String FROMAT = "MM-dd";

//	private XYMultipleSeriesDataset dataset;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chart_step_easy);
		
		GraphicalView chart1 = getSetpGraphicalView();

		LinearLayout barchart = (LinearLayout) findViewById(R.id.chartstep);
		
		barchart.addView(chart1,new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));

//		chart.addZoomListener(new zoomChange(), true, true);
		// chart.get
		
	}

	private GraphicalView getSetpGraphicalView(){
		String[] titles = new String[] { "每日步数" };

		LineChartInfo lineChart = DigitalHealthClient.getDailyStep(Utils.getUsername(this));
		// setting X
		long[] x = lineChart.getX();

		// setting Y
		double[] y = lineChart.getY();

		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		dataset = buildDataset(dataset,titles, x, y);
		
		int[] colors = new int[] { Color.YELLOW };
		PointStyle[] styles = new PointStyle[] { PointStyle.SQUARE};
		
		XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles, true);

		setChartSettings(renderer, "步数趋势图", "日", "步数(千)", lineChart.getMinX(), lineChart.getMaxX(),
				lineChart.getMinY(), lineChart.getMaxY(), Color.LTGRAY, Color.LTGRAY);
		renderer.setXLabels(9);
		renderer.setYLabels(10);
		renderer.setShowGrid(true);
		renderer.setXLabelsAlign(Align.CENTER);
		renderer.setYLabelsAlign(Align.RIGHT);
		renderer.setMargins(new int[]{20,20,20,20});
		// renderer.getSeriesRendererAt(0).setDisplayChartValues(true);
//		renderer.setZoomButtonsVisible(true);
//		以下两个限制加上后x轴不可调整
//		renderer.setPanLimits(new double[] { -10, 2000, -10, 2000 });
//		renderer.setZoomLimits(new double[] { -10, 2000, -10, 2000 });

		GraphicalView chart = ChartFactory.getTimeChartView(this, dataset, renderer, FROMAT);
		return chart;
	}
	protected XYMultipleSeriesDataset buildDataset(XYMultipleSeriesDataset dataset,String[] titles, long[] xValues,
			double[] yValues) {

		if (dataset == null) {
			dataset = new XYMultipleSeriesDataset();
		}

		int length = titles.length; // 有几条线
		XYSeries series = new XYSeries(titles[0]); // 根据每条线的名称创建
		
		int seriesLength = xValues.length; // 有几个点

		for (int k = 0; k < seriesLength; k++) // 每条线里有几个点
		{
			series.add(xValues[k], yValues[k]);
		}

			dataset.addSeries(series);

		return dataset;
	}

	protected XYMultipleSeriesRenderer buildRenderer(int[] colors, PointStyle[] styles, boolean fill) {
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		int length = colors.length;
		for (int i = 0; i < length; i++) {
			XYSeriesRenderer r = new XYSeriesRenderer();
			r.setColor(colors[i]);
			r.setPointStyle(styles[i]);
			r.setFillPoints(fill);
			renderer.addSeriesRenderer(r);
			//如果有2个以上的图形是相邻的设为不同的背景色
			if(i%2 == 0){
				// 设置页边空白的颜色
//		        renderer.setMarginsColor(Color.CYAN);
			}
		}
		return renderer;
	}

	protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle,
			String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor,
			int labelsColor) {
		renderer.setChartTitle(title);
		renderer.setXTitle(xTitle);
		renderer.setYTitle(yTitle);
		renderer.setXAxisMin(xMin);
		renderer.setXAxisMax(xMax);
		renderer.setYAxisMin(yMin);
		renderer.setYAxisMax(yMax);
		renderer.setAxesColor(axesColor);
		renderer.setLabelsColor(labelsColor);
	}

	/*
	 * 缩放监听处理
	 */
	private class zoomChange implements ZoomListener {
		@Override
		public void zoomApplied(ZoomEvent arg0) {
			// TODO Auto-generated method stub
			if (arg0.isZoomIn()) {
				// chart.
//				dataset = null;
			}
		}

		@Override
		public void zoomReset() {
			// TODO Auto-generated method stub

		}
	}

}